//
//  CEOBusXVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/4/17.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "CEOBusXVC.h"

#import "CeoBusXCell.h"
#import "RightButtonVC.h"
#import "TodoHttpManager.h"
#import "LXAlertView.h"
@interface CEOBusXVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)NSMutableArray *piXArr;
@end

@implementation CEOBusXVC

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDCeoMeBusPeiShenX:) name:TDCeoMeBusPeiShenX object:nil];
    
    [_manager userCeoBusPeiShenPXWithID:_piID];
    
    
    
    //TODO:审批状态,驳回或者同意
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDCeoMeBusPeiDingSP:) name:TDCeoMeBusPeiDingSP object:nil];
    
    
}

-(void)didTDCeoMeBusPeiShenX:(NSNotification *)notifo
{
    if (notifo.object !=0)
    {
       _piXArr = [notifo.object objectForKey:@"data"];
        [_tabView reloadData];
    }
}


//TODO:审批的状态
-(void)didTDCeoMeBusPeiDingSP:(NSNotification *)noyti
{
    if (noyti.object != nil)
    {
        [self.navigationController popViewControllerAnimated:NO];
    }
}




-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoMeBusPeiShenX object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDCeoMeBusPeiDingSP object:nil];
    
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
   self.title = @"商业价格表";
    self.view.backgroundColor = BgColor;
     [self setUIJiaHeader];
    
    _piXArr = [NSMutableArray array];

    _manager = [TodoHttpManager sharedManager];
    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight+42, B_WIDTH, B_HEIGHT-(LL_StatusBarAndNavigationBarHeight+42)) style:UITableViewStylePlain];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 40;
    _tabView.backgroundColor = BgColor;
    _tabView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tabView];
   
   
    //导航右侧的定价说明按钮
    UIButton * button_back = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60.0f, 20.0f)];
    [button_back setCenter:CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2)];
    [button_back.layer setCornerRadius:4.0f];
    [button_back.layer setBorderColor:[UIColor whiteColor].CGColor];
    [button_back.layer setBorderWidth:1.0f];
    button_back.titleLabel.font = [UIFont systemFontOfSize:12];
    [button_back setTitleColor:[UIColor whiteColor] forState:0];
    [button_back setTitle:@"定价说明" forState:UIControlStateNormal];
    [button_back.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [button_back addTarget:self action:@selector(dingJiaBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button_back];
    [backButton setStyle:UIBarButtonItemStyleDone];
    [self.navigationItem setRightBarButtonItem:backButton];




}

-(void)setUIJiaHeader
{
    UIView *aVi = [[UIView alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, B_WIDTH, 40)];
    aVi.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:aVi];
    NSArray *titArr = @[@"楼号",@"房号",@"楼层",@"面积",@"单价"];
    UILabel * tempView = [[UILabel alloc]init];
    NSInteger count = 5;//设置一排view的个数
    NSInteger margin = 10;//设置相隔距离
    NSInteger height = 20;//设置view的高度
    for (int i = 0; i < count; i ++) {
        //UIView * view = [[UIView alloc]init];
        //view.backgroundColor = [UIColor brownColor];
        UILabel *lab = [[UILabel alloc] init];
        lab.textColor = [UIColor orangeColor];
        lab.font = [UIFont systemFontOfSize:12];
        lab.textAlignment = NSTextAlignmentCenter;
        lab.text = titArr[i];
        [aVi addSubview:lab];
        
        
        //[aVi addSubview:view];
        if (i == 0) {
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(aVi).offset(margin);
                make.centerY.equalTo(aVi);
                make.height.mas_equalTo(height);
            }];
        }
        else if (i == count -1){
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(aVi).offset(-margin);
                make.left.equalTo(tempView.mas_right).offset(margin);
                make.centerY.equalTo(tempView);
                make.height.equalTo(tempView);
                make.width.equalTo(tempView);
            }];
        }
        else{
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(tempView.mas_right).offset(margin);
                make.centerY.equalTo(tempView);
                make.height.equalTo(tempView);
                make.width.equalTo(tempView);
            }];
        }
        tempView = lab;
        [lab layoutIfNeeded];
    }
    
    
    
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _piXArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static  NSString *ID = @"Cell";
    CeoBusXCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell)
    {
        cell = [[CeoBusXCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    if ([cell isKindOfClass:[CeoBusXCell class]])
    {
        if (_piXArr.count !=0)
        {
            NSDictionary *dict = [_piXArr objectAtIndex:indexPath.row];
            [(CeoBusXCell *)cell setPeiBusPiXCellWithLouLab:dict[@"block_num"] withFangLab:dict[@"room_num"]  withLoeCeng:dict[@"floor"]  withArea:dict[@"sale_area"]  withDianJ:dict[@"shenpi_price"]];
        }
        
    }
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 40)];
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
    
    UILabel *linLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, 0.5)];
    linLab.backgroundColor = [UIColor lightGrayColor];
    [footView addSubview:linLab];
    UIButton *agreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    agreeBtn.frame = CGRectMake(20, 5, 80, 30);
    agreeBtn.layer.cornerRadius = 5;
    agreeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [agreeBtn setTitle:@"同意执行" forState:0];
    [agreeBtn setTitleColor:[UIColor whiteColor] forState:0];
    [agreeBtn setBackgroundColor:[UIColor orangeColor]];
    [agreeBtn addTarget:self action:@selector(bussAgreeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:agreeBtn];
    
    
    UIButton *unAgreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    unAgreeBtn.layer.cornerRadius = 5;
    unAgreeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [unAgreeBtn setTitle:@"发回修改" forState:0];
    [unAgreeBtn setTitleColor:[UIColor whiteColor] forState:0];
    [unAgreeBtn setBackgroundColor:[UIColor orangeColor]];
    [unAgreeBtn addTarget:self action:@selector(bussUnAgreeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:unAgreeBtn];
    [unAgreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 30));
        make.right.equalTo(footView.mas_right).offset(-20);
        make.top.equalTo(footView.mas_top).offset(5);
    }];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if ([_busTypeStr  isEqualToString:@"待审批"])
    {
        return 40;
    }
    else
    {
        return 0;
    }
    
}


-(void)bussAgreeBtn:(UIButton *)btn
{
    LXAlertView *agreeView = [[LXAlertView alloc] initWithTitle:@"提示" message:@"同意执行?" cancelBtnTitle:@"取消" otherBtnTitle:@"确认" clickIndexBlock:^(NSInteger clickIndex) {
        
        if (clickIndex == 1)
        {
            NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
            
            [_manager userCeoBusPeiDJSPiWithID:_piID withUserID:IDA withType:@"2" withXiuYiJ:nil];
            
            [self.navigationController popViewControllerAnimated:NO];
        }
        
        
        
    }];
    [agreeView showLXAlertView];
    
   
    
}
-(void)bussUnAgreeBtn:(UIButton *)btn
{
    LXAlertView *agreeView = [[LXAlertView alloc] initWithTitle:@"提示" message:@"驳回修改?" cancelBtnTitle:@"取消" otherBtnTitle:@"确认" clickIndexBlock:^(NSInteger clickIndex) {
        
        if (clickIndex == 1)
        {
            NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
            
            [_manager userCeoBusPeiDJSPiWithID:_piID withUserID:IDA  withType:@"3" withXiuYiJ:nil];
            

            [self.navigationController popViewControllerAnimated:NO];
        }
        
        
        
    }];
    [agreeView showLXAlertView];
    
    
    
}


-(void)dingJiaBtnClick
{
    RightButtonVC *rightVC = [[RightButtonVC alloc] init];
    rightVC.shenID = _piID;
    [self.navigationController pushViewController:rightVC animated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
